package com.wangzy.game;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;


/**
 * @ProjectName: SportBS
 * @Date: 2022/8/22
 * @Desc:
 */
public class Boomb extends Sprite {

    public Boomb(Point point,BaseGameScene gameScene, Bitmap... frames) {
        super(point,gameScene, frames);
    }

    @Override
    public void update(Canvas canvas, Paint paint) {
        super.update(canvas, paint);
        if (alive && null != currentFrame && !currentFrame.isRecycled()) {
            canvas.drawBitmap(currentFrame, sourceRect, (Rect) gameRect.toRect(), paint);
        }
    }

    private ValueAnimator valueAnimator;

    public void explode(boolean withSound) {

        if(withSound){
//            SoundPlayUtils.play(7);
        }

        valueAnimator = ValueAnimator.ofFloat(0, frames.length - 1);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                try {
                    float p = (float) animation.getAnimatedValue();
                    currentFrame = frames[(int) p];
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                dead();
            }
        });
        valueAnimator.start();
    }

}